﻿using System;
using System.Data;
using System.Collections.Generic;
using Chenxin.Lib.Business.Entity;
namespace Chenxin.Lib.Business.BLL
{
    /// <summary>
    /// 业务逻辑类ContractCommisionAdjustInfo 的摘要说明。
    /// </summary>
    public class ContractCommisionAdjustInfo
    {
        public ContractCommisionAdjustInfo()
        { }
        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo model)
        {
            Chenxin.Lib.Business.DAL.ContractCommisionAdjustInfo.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo model)
        {
            Chenxin.Lib.Business.DAL.ContractCommisionAdjustInfo.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(Guid RecordId)
        {

            Chenxin.Lib.Business.DAL.ContractCommisionAdjustInfo.Delete(RecordId);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo GetModel(Guid RecordId)
        {

            return Chenxin.Lib.Business.DAL.ContractCommisionAdjustInfo.GetModel(RecordId);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return Chenxin.Lib.Business.DAL.ContractCommisionAdjustInfo.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo> GetModelList(string strWhere)
        {
            DataSet ds = Chenxin.Lib.Business.DAL.ContractCommisionAdjustInfo.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo> DataTableToList(DataTable dt)
        {
            List<Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo> modelList = new List<Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Chenxin.Lib.Business.Entity.ContractCommisionAdjustInfo();
                    if (dt.Rows[n]["RecordId"].ToString() != "")
                    {
                        model.RecordId = new Guid(dt.Rows[n]["RecordId"].ToString());
                    }
                    if (dt.Rows[n]["ContractId"].ToString() != "")
                    {
                        model.ContractId = new Guid(dt.Rows[n]["ContractId"].ToString());
                    }
                    model.ContractCode = dt.Rows[n]["ContractCode"].ToString();
                    if (dt.Rows[n]["ContractCityId"].ToString() != "")
                    {
                        model.ContractCityId = new Guid(dt.Rows[n]["ContractCityId"].ToString());
                    }
                    model.ContractCity = dt.Rows[n]["ContractCity"].ToString();
                    if (dt.Rows[n]["ContractCompanyId"].ToString() != "")
                    {
                        model.ContractCompanyId = new Guid(dt.Rows[n]["ContractCompanyId"].ToString());
                    }
                    model.ContractCompany = dt.Rows[n]["ContractCompany"].ToString();
                    if (dt.Rows[n]["AdjustType"].ToString() != "")
                    {
                        model.AdjustType = int.Parse(dt.Rows[n]["AdjustType"].ToString());
                    }
                    model.AdjustReason = dt.Rows[n]["AdjustReason"].ToString();
                    if (dt.Rows[n]["DiscountType"].ToString() != "")
                    {
                        model.DiscountType = int.Parse(dt.Rows[n]["DiscountType"].ToString());
                    }
                    model.AdjustDescription = dt.Rows[n]["AdjustDescription"].ToString();
                    if (dt.Rows[n]["AdjustMoney"].ToString() != "")
                    {
                        model.AdjustMoney = decimal.Parse(dt.Rows[n]["AdjustMoney"].ToString());
                    }
                    if (dt.Rows[n]["BeforeAdjust"].ToString() != "")
                    {
                        model.BeforeAdjust = decimal.Parse(dt.Rows[n]["BeforeAdjust"].ToString());
                    }
                    if (dt.Rows[n]["AfterAdjust"].ToString() != "")
                    {
                        model.AfterAdjust = decimal.Parse(dt.Rows[n]["AfterAdjust"].ToString());
                    }
                    if (dt.Rows[n]["Addtime"].ToString() != "")
                    {
                        model.Addtime = DateTime.Parse(dt.Rows[n]["Addtime"].ToString());
                    }
                    if (dt.Rows[n]["AddEmployeeId"].ToString() != "")
                    {
                        model.AddEmployeeId = new Guid(dt.Rows[n]["AddEmployeeId"].ToString());
                    }
                    model.AddEmployee = dt.Rows[n]["AddEmployee"].ToString();
                    if (dt.Rows[n]["AddDepartId"].ToString() != "")
                    {
                        model.AddDepartId = new Guid(dt.Rows[n]["AddDepartId"].ToString());
                    }
                    model.AddDepart = dt.Rows[n]["AddDepart"].ToString();
                    if (dt.Rows[n]["AddCompanyId"].ToString() != "")
                    {
                        model.AddCompanyId = new Guid(dt.Rows[n]["AddCompanyId"].ToString());
                    }
                    model.AddCompany = dt.Rows[n]["AddCompany"].ToString();
                    model.AddCity = dt.Rows[n]["AddCity"].ToString();
                    if (dt.Rows[n]["AddCityId"].ToString() != "")
                    {
                        model.AddCityId = new Guid(dt.Rows[n]["AddCityId"].ToString());
                    }
                    if (dt.Rows[n]["Status"].ToString() != "")
                    {
                        model.Status = int.Parse(dt.Rows[n]["Status"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 根据成交编号获得数据列表
        /// </summary>
        public DataSet GetListByContractCode(Dictionary<string,string> dicParams)
        {
            System.Text.StringBuilder strWhere = new System.Text.StringBuilder();
            int PageSize = 10;
            int PageIndex = 1;

            string TableName = "t_Contract_CommisionAdjustInfo";
            string OrderField = "t_Contract_CommisionAdjustInfo.Addtime DESC";
            string FieldList = "ContractCode,ContractCompany,AdjustReason,DiscountType,AdjustDescription,AdjustType,DiscountType,AdjustMoney,BeforeAdjust," +
                               "AfterAdjust,Addtime,AddDepart,AddEmployee";
            string TableFieldList = "ContractCode,ContractCompany,AdjustReason,AdjustDescription,AdjustMoney,BeforeAdjust,AfterAdjust,AddDepart,AddEmployee," +
                                    "(Case When AdjustType = 10 Then '上家确认收入' When AdjustType = 20 Then '下家确认收入' When AdjustType = 30 Then '上家咨询费' Else '下家咨询费' End) As AdjustType," +
                                    "(Case When DiscountType = 10 Then '事前折佣' When DiscountType = 20 Then '事后折佣' Else '' End) As DiscountType," +
                                    "REPLACE(CONVERT(Varchar(10),Addtime,111),'/','-') As Addtime";

            strWhere.Append(" 1=1 ");
            if (dicParams.ContainsKey("PageSize"))
            {
                PageSize = Convert.ToInt32(dicParams["PageSize"]);
            }
            if (dicParams.ContainsKey("PageIndex"))
            {
                PageIndex = Convert.ToInt32(dicParams["PageIndex"]) + 1;
            }
            if (dicParams.ContainsKey("Sort"))
            {
                OrderField = dicParams["Sort"];
            }
            if (dicParams.ContainsKey("ContractCode"))
            {
                strWhere.AppendFormat("And ContractCode='{0}'", dicParams["ContractCode"]);
            }

            return Chenxin.Lib.Business.DAL.ContractCommisionAdjustInfo.GetPartList(PageSize, PageIndex, OrderField, TableName, strWhere.ToString(), FieldList, TableFieldList, true);
        }

        #endregion  成员方法
    }
}

